package com.zhaoyh.framework.taglib;


import com.zhaoyh.framework.code.model.PageBean;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;

/**
 * <p>
 * Title: 信息管理系统通用平台
 * </p>
 * <p>
 * Description: Information Management System Universal Platform
 * </p>
 * <p>
 * Copyright: Copyright (c) 2011
 * </p>
 * 
 * @author cmore
 * @version 1.0
 */
@SuppressWarnings("serial")
public class PageSpanTag extends BodyTagSupport {

	private String confirm = "document.getElementById('turnPage').click();";
	private String beanName = null;

	public PageSpanTag() {
	}

	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			if (this.beanName == null || "".equals(this.beanName))
				this.beanName = "pageBean";
			PageBean pageBean = (PageBean) pageContext.getRequest().getAttribute(this.beanName);
			if (pageBean == null) {
				return super.doEndTag();
			}
			if (pageBean.getDataSum() == 0) {
				return super.doEndTag();
			}
			StringBuilder bfString = new StringBuilder("");
			// 提交按钮
			bfString.append("<input type='submit' style='display:none' name='turnPage' id='turnPage'>");
			// 隐藏属性
			bfString.append("<input type='hidden' id='pageNumber' name='pageNumber' value='"
					+ pageBean.getPageNumber() + "'/>");
			// 上一页
			if (pageBean.getPageNumber() - 1 <= 0) {
				bfString.append("<span id='s-first' style='color:#d5d5d5;'>上一页</span>");
			} else {
				bfString.append("<span id='s-first' onclick=\"document.getElementById('pageNumber').value="
						+ (pageBean.getPageNumber() - 1) + ";" + confirm + "\">上一页</span>");
			}
			// 第一页
			if (pageBean.getPageNumber() != 1)
				bfString.append(
						"<span onclick=\"document.getElementById('pageNumber').value=1;" + confirm + "\">1</span>");
			if (pageBean.getPageNumber() > 4)
				bfString.append("...&nbsp;&nbsp;&nbsp;");
			// 当前页前2个页码
			for (int i = pageBean.getPageNumber() - 2, c = 2; c > 0; i++, c--) {
				if (i < 2)
					continue;
				bfString.append("<span onclick=\"document.getElementById('pageNumber').value=" + i + ";" + confirm
						+ "\">" + i + "</span>");
			}
			// 当前页
			bfString.append("<span class='cur-s-page'>" + pageBean.getPageNumber() + "</span>");
			// 当前页后2个页码
			for (int i = pageBean.getPageNumber() + 1, c = 2; i < pageBean.getPageSum() && c > 0; i++, c--)
				bfString.append("<span onclick=\"document.getElementById('pageNumber').value=" + i + ";" + confirm
						+ "\">" + i + "</span>");
			// ...
			if (pageBean.getPageNumber() + 3 < pageBean.getPageSum())
				bfString.append("...&nbsp;&nbsp;&nbsp;");
			// 最后一页
			if (pageBean.getPageNumber() != pageBean.getPageSum())
				bfString.append("<span onclick=\"document.getElementById('pageNumber').value=" + pageBean.getPageSum()
						+ ";" + confirm + "\">" + pageBean.getPageSum() + "</span>");
			// 下一页
			if (pageBean.getPageNumber() >= pageBean.getPageSum()) {
				bfString.append("<span id='s-last' style='color:#d5d5d5;'>下一页</span>");
			} else {
				bfString.append("<span id='s-last' onclick=\"document.getElementById('pageNumber').value="
						+ (pageBean.getPageNumber() + 1) + ";" + confirm + "\">下一页</span>");
			}

			pageContext.getOut().print(bfString.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

}